package li.teng.android.xplanet;

import java.util.List;

import li.teng.android.xplanet.content.MyPosition;
import li.teng.android.xplanet.content.MyPositionHelper;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class PositionList extends ListActivity
implements SharedPreferences.OnSharedPreferenceChangeListener,
View.OnClickListener {
	private ListView listView = null;
	private long trackId = -1;
	private int contextPosition = -1;
	List<MyPosition> lstMyPosition;
	
	public static Location m_InitLocation;
	public static int m_Distance = 5;
	private SharedPreferences sharedPreferences;
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}
	
	@Override
	public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.positions);
		if(m_InitLocation != null)
			((TextView)findViewById(R.id.message)).setText("pos=" + m_InitLocation.getLongitude() + "/" + m_InitLocation.getLatitude());
		//        myListView = (ListView)findViewById(R.id.lstPositions);
		//        List<MyPosition> lst = MyPositionHelper.getInstance(this).getAllPositions();
		//        ArrayAdapter aa = new ArrayAdapter<MyPosition>(getApplicationContext(), android.R.layout.simple_list_item_1, lst);   	
		//        myListView.setAdapter(aa);
		

		sharedPreferences = getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
//		if (sharedPreferences != null) {
//		      sharedPreferences.registerOnSharedPreferenceChangeListener(this);
//		}	
		int distance = sharedPreferences.getInt(getString(R.string.min_recording_distance_key), -1);	
		lstMyPosition = MyPositionHelper.getInstance(this).getPositionsNearBy(m_InitLocation, distance);
        ArrayAdapter aa = new ArrayAdapter<MyPosition>(getApplicationContext(), android.R.layout.simple_list_item_1, lstMyPosition);   	
        setListAdapter(aa);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
	    Intent result = new Intent();
	    //if(v.getTag() == null)
	    //Log.d(XplanetConstants.TAG, "onListItemClick tag = null");
	    //;
	    long objId = ((MyPosition)(lstMyPosition.get((int)id))).getId();
	    result.putExtra("position_id", objId);
	    setResult(XplanetConstants.SHOW_LIST, result);
	    finish();
	}
	


	private final OnCreateContextMenuListener contextMenuListener =
		new OnCreateContextMenuListener() {
		@Override
		public void onCreateContextMenu(ContextMenu menu, View v,
				ContextMenuInfo menuInfo) {
			menu.setHeaderTitle(R.string.positionlist_title);
			AdapterView.AdapterContextMenuInfo info =
				(AdapterView.AdapterContextMenuInfo) menuInfo;

			contextPosition = info.position;
			trackId = listView.getAdapter().getItemId(
					contextPosition);
			menu.add(0, XplanetConstants.MENU_SHOWWIKI, 0,
					R.string.show_wiki);
			menu.add(0, XplanetConstants.MENU_GETPOSITION, 0,
					R.string.show_map);

		}
	};

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if (!super.onMenuItemSelected(featureId, item)) {
			switch (item.getItemId()) {
			case XplanetConstants.MENU_SHOWWIKI: {
				onListItemClick(null, null, 0, trackId);
				return true;
			}
			case XplanetConstants.MENU_GETPOSITION: {
				Intent intent = new Intent(this, PositionMap.class);
				intent.putExtra("trackid", trackId);
				startActivity(intent);
				return true;
			}
			default: {
				Intent result = new Intent();
				result.putExtra("trackid", trackId);
				//setResult(XplanetConstants.getActionFromMenuId(item.getItemId()), result);
				finish();
				return true;
			}
			}
		}
		return false;
	}
}
